package main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Vector;

import mt.BaiduMT;
import mt.C2EMT;
import strategy.FeatureCalc;

public class Trash {
	public static void change(String[] args) throws Exception {
		BufferedReader r = U.newReader("c:/a.txt");
		BufferedWriter w = U.newWriter("c:/b.txt");
		while (true) {
			String l = r.readLine();
			if (l == null)
				break;
			if (!l.startsWith("<seg id="))
				continue;
			int begin = l.indexOf('>') + 1;
			int end = l.lastIndexOf('<');
			w.write(l.substring(begin, end).replace(" ", "") + "\n");
		}
		w.close();
		r.close();
	}

	public static void main(String[] args) throws Exception {
		System.out.println("g");
		getFilePPL("./data/mtRes/google.txt","./data/mtRes/googleLOGP.txt");
		System.out.println("B");
		getFilePPL("./data/mtRes/baidu.txt","./data/mtRes/baiduLOGP.txt");
		System.out.println("Y");
		getFilePPL("./data/mtRes/youdao.txt","./data/mtRes/youdaoLOGP.txt");
//		normalize();
	}

	public static void evaluate() throws Exception {
		C2EMT.toTst("./data/mtRes/baidu.txt", "./data/evaData/tst");
		// 接下来自己去执行 data/evaData/bat.bat
	}

	public static void parseBaidu() throws Exception {
		Vector<String> v = U.file2vector("./data/src.txt");
		BaiduMT b = new BaiduMT();
		for (int i = 565; i < v.size(); i++) {
			System.out.println(i);
			U.appendStringToFile(b.translate(v.get(i))+ "\n","./data/mtRes/baidu.txt");
			if(i%50==0)
				b=new BaiduMT();
		}
	}

	//调研logP 与句子长度的相关性
	public static void corelation() throws Exception {
		Vector<String> v =U.file2vector("./data/mtRes/youdao.txt");
		BufferedWriter w =U.newWriter("./a.txt");
		int i =0;
		for(String s :v){
			int l =Util.getSentenceLength(s);
			double logpdn = FeatureCalc.getLmLogPSingle(s);
			w.write(l+"\t"+logpdn+"\n");
			i++;
			if(i%50==0)
				System.out.println(i);
		}
		w.close();
		
	}


	public static void getFilePPL(String in, String out) throws Exception{
		Vector<String> v =U.file2vector(in);
		BufferedWriter w =U.newWriter(out);
		int i =1;
		for(String s :v){
			w.write(FeatureCalc.getLmLogPSingle(s)+"\n");
			if((i++)%100==0)
				System.out.println(i);
			
		}
		w.close();
	}

	public static void splitFile(String in ,String out ) throws Exception{
		Vector<String> v =U.file2vector(in);
		BufferedWriter r =U.newWriter(out);
		
		for(String s:v)
			r.write(C2EMT.segment(s)+"\n");
		r.close();
	}

	public static void normalize() throws Exception{
		String g="./data/mtRes/googleLOGP.txt";
		String b="./data/mtRes/baiduLOGP.txt";
		String y="./data/mtRes/youdaoLOGP.txt";
		String go="./data/mtRes/go.txt";
		String bo="./data/mtRes/bo.txt";
		String yo="./data/mtRes/yo.txt";
		
		Vector<Double> gv = new Vector<Double>();
		Vector<Double> bv = new Vector<Double>();
		Vector<Double> yv = new Vector<Double>();
		
		//count ave
		Vector<String> v =U.file2vector(g);
		for(String s :v)
			gv.add(Double.parseDouble(s));
		v =U.file2vector(b);
		for(String s :v)
			bv.add(Double.parseDouble(s));
		v =U.file2vector(y);
		for(String s :v)
			yv.add(Double.parseDouble(s));
		
		double ave=0;
		for(double d:gv)
			ave +=d;
		for(double d:bv)
			ave +=d;
		for(double d:yv)
			ave +=d;
		ave =ave /v.size()/3;
		System.out.println("ave:" + ave);
		
		assert(gv.size()==bv.size()&&bv.size()==yv.size());
		//count std
		double std =0;
		for(int i=0;i<gv.size();i++){
			gv.set(i, gv.get(i)-ave);
			bv.set(i, bv.get(i)-ave);
			yv.set(i, yv.get(i)-ave);
			std +=gv.get(i)*gv.get(i)+bv.get(i)*bv.get(i)+yv.get(i)*yv.get(i);
		}
		std =Math.pow(std/v.size()/3,0.5);
		System.out.println("std:" + std);
		
		for(int i=0;i<gv.size();i++){
			gv.set(i, gv.get(i)/std);
			bv.set(i, bv.get(i)/std);
			yv.set(i, yv.get(i)/std);
		}
		U.vector2file(gv,go);
		U.vector2file(bv,bo);
		U.vector2file(yv,yo);
		
	}
}
